Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
In [1]:
from collections import defaultdict
import numpy as np
In [2]:
class FenwickTree:
def __init__(self, n):
self._tree = defaultdict(lambda: 0)
self._n = n
@staticmethod
def lsb(x):
return x & (-x)
def update(self, position, key):
while position <= self._n:
self._tree[position] += key
position += FenwickTree.lsb(position)
def query(self, position):
suma = 0
while position > 0:
suma += self._tree[position]
position -= FenwickTree.lsb(position)
return suma
In [3]:
def count_inversions(xs, max_size=2**31):
aib = FenwickTree(max_size)
inversions = 0
for i, x in enumerate(xs):
inversions += i - aib.query(x)
aib.update(x, 1)
return inversions
In [4]:
count_inversions([3, 2, 1])
Out[4]:
In [5]:
count_inversions(range(10, 0, -1))
Out[5]:
In [6]:
count_inversions([3, 4, 1, 2, 5])
Out[6]:
In [7]:
xs = np.random.randint(1, 100 + 1, size=20)
print(xs)
count_inversions(xs)
Out[7]:
In [8]:
xs = np.random.randint(1, 100 + 1, size=50)
print(xs)
count_inversions(xs)
Out[8]:
In [9]:
count_inversions(np.random.randint(1, 1000, size=1000000), max_size=1000)
Out[9]: